/* eslint-disable */
import Vue from 'vue';
import VueRouter, { RouteConfig, Route } from 'vue-router';

import store from '@/store';


import homeRouter from './home.router';
import passportrouter from './passport.router';

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  ...homeRouter,
  ...passportrouter,
];

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

/**
 * 设置全局路由守卫
 */
router.beforeResolve((to: Route, from: Route, next: any) => {
  const state: any = store.state;
  const user: any = state.user;
  console.log('user', user);
  console.log('to', to);
  if (to.meta && to.meta.routerGuard) {
    // 需要路由守护
    console.log('user.token', user.token);
    if (user.token === undefined) {
      next({ name: '/passport/login', query: {
        redirect: to.path,
      } });
      return;
    }
  }

  next();
});

router.beforeEach( ( to, from, next ) => {
  next();
});

router.afterEach( ( to: any, from: any) => {
  // const tabInfo: any = {
  //   name: to.name,
  //   closable: true,
  //   path: to.name,
  //   title: to.meta.title,
  //   activeName: from.name,
  //   i18n: to.meta.i18n || null,
  // };

  // // 设置复用tab
  // store.dispatch('reuseTab/add', tabInfo);
  // // 设置标题
  // store.commit('app/changeTitle', {
  //     title: tabInfo.title,
  //     i18n: tabInfo.i18n,
  //   },
  // );
});

export default router;
